package com.funcity.net
{
	import com.funcity.events.PacketEvent;
	
	import flash.net.ObjectEncoding;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	public final class SecureConnection extends Connection
	{
		public function SecureConnection()
		{
			super();
		}
		
		public override function send(flag:uint, packet:ByteArray):void
		{
			var buffer:ByteArray = new ByteArray();
			buffer.endian = Endian.BIG_ENDIAN;
			buffer.objectEncoding = ObjectEncoding.AMF3;
			
			/*
			 * security algorithm for packet, it is also should be taken as a checksum
			 * packet information should be taken as algorithm parameter
			 * for saving band width, packet should be compressed
			 * other auxiliary infrastructure such as packet delimiter also could be used
			 */
			packet.compress();
			buffer.writeBytes(packet);
			
			/*
			 * buffer.writeUnsignedInt(packet.length ^ getTimer());
			 * buffer.writeByte(0xFF);
			 */
			super.send(flag, buffer);
		}
		
		protected override function readPacket(flag:uint, buffer:ByteArray):void
		{
			dispatchEvent(new PacketEvent(buffer));
		}
	}
}